$(function () {
    var roomid="";
    var token="";
    var  teacherCode ="";
    var  ptcode ="";
    listLive(1);

    $('body').on('click', '#btn1, #btn2', function () {
        listLive(1);
    }).on('click', '#live-list-context i.fa-trash', function () {
        var parent = $(this).parents('div:eq(0)');
        icModale(2, '确认关闭选中的直播间吗?', '', function () {
            deleteLive(parent, parent.data('room'));
        })
    }).on('click', '.openuser', function () {
        openUser($(this));
    }).on('click', "#copyUrl", function () {
        var roomid = $(this).data('roomid');
        copyUrl(roomid);
    });

    function copyUrl(roomid) {
        var shareUrl_ = $("#shareUrl_"+roomid);
        shareUrl_.select();
        document.execCommand("Copy");
        layer.msg("已复制")
    };

    function openUser(that) {


        //今日直播调起选择直播身份和下载直播客户端
        liverOpenOptionsFn();

        roomid= that.data('playurl');
        teacherCode = that.data('teachercode');
        token=that.data('admincode');
        ptcode=that.data('ptcode')

    }


    //今日直播调起选择直播身份和下载直播客户端
    function liverOpenOptionsFn() {
        var _ele = $('#sysItemLiveTipsEle').val()
        parent.layer.open({
            type: 1,
            area: ['860px', '600px'],
            content: _ele,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '今日直播',
            success: function (layero, index) {
                layero.find('[data-toggle="tooltip"]').tooltip();
                layero.find("#theHostLive").on("click",function(){
                    openInputName(1);
                });
                layero.find("#theTeacherLive").on("click",function(){
                    openInputName(2);
                });
                layero.find("#theTaLive").on("click",function(){
                    openInputName(3);
                });
            }
        });
    }


    function  openInputName(type){
        var _ele = $('#openInputName').val()

        parent.layer.open({
            type: 1,
            area: ['240px','180px'],
            skin: 'layui-layer-demo',
            title: '<strong>请输入姓名</strong>',
            content: _ele,
            btn: '确定',
            yes: function(index, layero) {
                var pass = layero.find('#rejectedCause').val();
                if(pass.length<=0){
                    icModale(0, '请填写姓名');
                    return  ;
                }
                if(pass.length>=30){
                    icModale(0, '姓名不能大于30个字符');
                    return  ;
                }
                icModale(0, '进入中');
                if(type==1){
                    var url ="https://view.csslcloud.net/api/view/index?roomid="+roomid+"&userid="+liveId+"&autoLogin=true&viewername="+pass+"&autoLogin=true&viewertoken="+ptcode;
                    window.open(url,"_blank");
                }
                if(type==2){
                    window.location.href =  "cclive://"+liveId+"/"+roomid+"/"+pass+"/"+teacherCode;
                }

                if(type==3){
                    var url ="https://view.csslcloud.net/api/view/assistant?viewername="+pass+"&roomid="+roomid+"&userid="+liveId+"&autoLogin=true&viewertoken="+token;
                    window.open(url,"_blank")
                }

                icModale(0, '进入中');
                parent.layer.closeAll();

            }
        });

    }


    function deleteLive(obj,room) {
        $.ajax({
            url: baseloaction + '/admin/course/ajaxLiveClose',
            data: {'room_id': room,"type":$(obj).data('service-type')},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    var _that =obj.parent('div').parent('div').parent('div');
                    obj.find('span').remove();
                    _that.find('.badge').removeClass("badge-info").addClass("badge-danger").text("已关闭");
                    icModale(0,"关闭成功，关闭后直播间不可用");
                } else {
                    icModale(0, response.message);
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        })
    }

    function listLive(currentPage) {
        var params = $("#search-form").serializeJSON();
        params.type =$("input[name='type']").val();
        params = JSON.stringify(params);
        ajaxGetListData({
            url: baseloaction + '/admin/course/ajaxLiveList',//请求URL
            contextId: 'live-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: currentPage,//页码
            pageCall: listLive, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-course', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").text('（共'+total+'个直播）');
            }
        });
    }

});